package com.mao.crowd.mvc.config;

import com.mao.crowd.service.api.AdminService;
import com.mao.crowd.service.api.AuthService;
import com.mao.crowd.service.api.RoleService;
import entity.Admin;
import entity.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: Administrator
 * @description:
 * @cteate 2021-06-09-23:52
 */
@Service
public class CrowdUserDetailsService implements UserDetailsService {

    @Autowired
    private AdminService adminService;

    @Autowired
    private RoleService roleService;

    @Autowired
    private AuthService authService;

    /**
     * 从各个表中,查出admin对象\角色\权限,并封装到securityAdmin
     * @param username
     * @return
     * @throws UsernameNotFoundException
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        Admin admin = adminService.getAdminByLoginAcct(username);

        Integer adminId = admin.getId();

        List<Role> assignedRoleList = roleService.getAssignedRole(adminId);

        List<String> authNameList = authService.getAssignedAuthNameByAdminId(adminId);

        List<GrantedAuthority> authorities = new ArrayList<>();

        for(Role role:assignedRoleList){
            String roleName ="ROLE_" + role.getName();

            SimpleGrantedAuthority simpleGrantedAuthority = new SimpleGrantedAuthority(roleName);

            authorities.add(simpleGrantedAuthority);
        }

        for (String authName:authNameList){
            SimpleGrantedAuthority simpleGrantedAuthority = new SimpleGrantedAuthority(authName);
            authorities.add(simpleGrantedAuthority);
        }

        SecurityAdmin securityAdmin = new SecurityAdmin(admin, authorities);
        return securityAdmin;
    }
}
